#ifndef THREAD_HPP
#define THREAD_HPP

#include <iostream>
#include <string>
#include <pthread.h>
#include <cstdio>
#include <cstring>
#include <functional>
#include "Log.hpp"

namespace ThreadModlue
{
    static uint32_t number = 1;

    using namespace LogModule;
    class Thread
    {
        using func_t = std::function<void()>;

    private:
        void EnableDetach()
        {
            std::cout << "线程被分离了" << std::endl;
            _isdetach = true;
        }

        void EnableRunning()
        {
            _isrunning = true;
        }

        static void *Routine(void *args)
        {
            Thread *self = static_cast<Thread *>(args);
            // self->EnableDetach();
            if (self->_isdetach)
                self->Detach();

            pthread_setname_np(self->_tid, self->_name.c_str());
            self->_func();

            return nullptr;
        }

    public:
        Thread(func_t func)
            : _tid(0),
              _isdetach(false),
              _isrunning(false),
              res(nullptr),
              _func(func)
        {
            _name = "Thread-" + std::to_string(number++);
        }

        void Detach()
        {
            if (_isdetach)
                return;
            if (_isrunning)
                pthread_detach(_tid);
            EnableDetach();
        }

        bool Start()
        {
            if (_isrunning)
                return false;
            int n = pthread_create(&_tid, nullptr, Routine, this);
            if (n != 0)
            {
                std::cerr << "Failed to create thread: " << _name << std::endl;
                return false;
            }
            else
            {
                std::cout << _name << " started" << std::endl;
            }
            return true;
        }
        std::string Name()
        {
            return _name;
        }
        bool Stop()
        {
            if (_isrunning)
            {
                int n = pthread_cancel(_tid);
                if (n != 0)
                {
                    std::cerr << "Failed to cancel thread: " << _name << std::endl;
                    return false;
                }
                else
                {
                    _isrunning = false;
                    std::cout << _name << " stopped" << std::endl;
                }
            }
            return true;
        }

        void Join()
        {
            if (_isdetach)
                return;
            int n = pthread_join(_tid, &res);
            if (n != 0)
            {
                LOG(LogLevel::ERROR) << "Failed to join thread: " << _name;
            }
            else
            {
                LOG(LogLevel::DEBUG) << _name << " joined";
            }
        }
        pthread_t Id()
        {
            return _tid;
        }
        ~Thread()
        {
        }

    private:
        pthread_t _tid;
        std::string _name;
        bool _isdetach;
        bool _isrunning;
        void *res;
        func_t _func;
    };
}

#endif